﻿//
// eCRAM, a monthly activity report web application
// Copyright (C) 2010  Aptea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System.Data.Linq;
using System.Linq;
using eCRAM.Model;
using System.Collections.Generic;

namespace eCRAM.Dao
{
    /// <summary>
    /// </summary>
    public class ActivityRepository :LinqRepository<Activity>, IActivityRepository
    {
        private Table<Activity> ActivitiesTable { get; set; }

        public ActivityRepository(DataContext dataContent)
            : base(dataContent)
        {
            ActivitiesTable = DataContext.GetTable<Activity>();
        }

        public override IQueryable<Activity> GetAll()
        {
            return (from activity in ActivitiesTable select activity);
        }

        /// <summary>
        /// Create of activity
        /// </summary>
        /// <param name="user"></param>
        public override void Create(Activity activity)
        {
            ActivitiesTable.InsertOnSubmit(activity);
            this.SubmitChanges();
        }

       /* /// <summary>
        /// Update activity
        /// </summary>
        /// <param name="user"></param>
        public override void Update(Activity activity)
        {
            Activity tmp = this.GetById(activity.Id);
            tmp.StartDate = activity.StartDate;
            tmp.EndDate = activity.EndDate;
            tmp.Type = activity.Type;
            tmp.Name = activity.Name;
            tmp.IsActive = activity.IsActive;
            this.SubmitChanges();
        }
        * */

        /// <summary>
        /// Delete activity
        /// </summary>
        /// <param name="user"></param>
        public override void Delete(Activity activity)
        {
            //ActivitiesTable.Attach(activity);
            ActivitiesTable.DeleteOnSubmit(activity);
            this.SubmitChanges();
        }

        /// <summary>
        /// Get activity by Id
        /// </summary>
        /// <param name="activityId"></param>
        /// <returns></returns>
        public override Activity GetById(System.Guid activityId)
        {
            Activity activity = (ActivitiesTable.Where(a => a.Id == activityId)).Single();
            return activity;
        }

        /// <summary>
        /// Get only activity list from start to limit 
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public override IQueryable<Activity> GetTop(int start, int limit)
        {
            IQueryable<Activity> activities = (
                                        from activity
                                        in ActivitiesTable
                                        select activity
                                     )
                                     .Skip(start)
                                     .Take(limit - start);
            return activities;
        }
        
        /// <summary>
        /// Get the Activities Count in db
        /// </summary>
        /// <returns></returns>
        public override int GetCount()
        {
            return (from activity in ActivitiesTable select activity).Count();
        }

        /// <summary>
        /// Tell us if activity is available to be createde or not (name must be unique)
        /// </summary>
        /// <param name="name"></param>
        /// <param name="activityId">activityId is not Empty, we just check if the name already exist 
        /// in db for an other activity, if not check if user name exist in db</param>
        /// <returns></returns>
        public bool ActivityNameIsAvailable(string name, System.Guid activityId)
        {
            bool isAvailable = true;
            IQueryable<Activity> activities;
            if (activityId == System.Guid.Empty)
            {
                activities = ActivitiesTable.Where(a => a.Name == name);
            }
            else
            {
                activities = ActivitiesTable.Where(a => (a.Name == name) && (a.Id != activityId));
            }

            if (activities.Count() != 0)
            {
                isAvailable = false;
            }
            return isAvailable;
        }

        public IList<string> GetAllActivityName()
        {
            return (from activity in ActivitiesTable select activity.Name).ToList<string>();
        }
    }
}
